/*

Server Side Validation Library: A C# library for validation.
Copyright (C) 2006  Chad Finsterwald, CoreWeb

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace CoreWebLibrary.Validation.Web
{
    public class InlineStyle : StyleItem
    {
        protected WebControl m_webControl;
        protected HtmlControl m_htmlControl;

        private string m_styleName;
        public virtual string StyleName
        {
            get { return m_styleName; }
            set { m_styleName = value; }
        }

        private string m_value;
        public virtual string Value
        {
            get { return m_value; }
            set { m_value = value; }
        }

        public InlineStyle(string styleName, 
            string value,
            params System.Type[] types)
        {
            StyleName = styleName;
            Value = value;

            if (types != null && types.Length > 0)
            {
                m_controlTypes = new List<Type>(types.Length);
                m_controlTypes.AddRange(types);
            }
            else
            {
                m_controlTypes = null;
            }
        }
        
        public override void ApplyStyle(bool isValid, string description, Control[] controls)
        {
            if (controls != null && controls.Length > 0)
            {
                m_isValid = isValid;
                m_description = description;

                Control c = null;

                for (int i = 0; i < controls.Length; i++)
                {
                    c = controls[i];
                    if (ControlTypes == null || ControlTypes.Contains(c.GetType()))
                    {
                        if (c is HtmlControl)
                            ApplyStyleToHtmlControl(c);
                        else if (c is WebControl)
                            ApplyStyleToWebControl(c);
                        else
                            throw new ApplicationException("Unexpected control type: " + c.GetType() + ".");
                    }
                }
            }
        }

        public override int GetHashCode()
        {
            int i = 0;
            if (ControlTypes != null)
            {
                foreach (System.Type t in ControlTypes)
                {
                    if ( t != null )
                        i ^= t.GetHashCode();
                }
            }
            return StyleName.GetHashCode() ^ i;
        }

        protected virtual void ApplyStyleToHtmlControl(Control c)
        {
            m_htmlControl = (HtmlControl)c;

            if (m_isValid)
            {
                m_htmlControl.Attributes.CssStyle.Remove(StyleName);
                RemoveHtmlControlToolTip();
            }
            else
            {
                m_htmlControl.Attributes.CssStyle.Add(StyleName, Value);
                DoHtmlControlToolTip();
            }
        }

        protected virtual void ApplyStyleToWebControl(Control c)
        {
            m_webControl = (WebControl)c;

            if (m_isValid)
            {
                m_webControl.Style.Remove(StyleName);
                RemoveWebControlToolTip();
            }
            else
            {
                m_webControl.Style.Add(StyleName, Value);
                DoWebControlToolTip();
            }
        }

        protected override void DoWebControlToolTip()
        {
            m_webControl.ToolTip = m_description; 
        }

        protected override void RemoveWebControlToolTip()
        {
            m_webControl.ToolTip = string.Empty;
        }

        protected override void DoHtmlControlToolTip()
        {

        }

        protected override void RemoveHtmlControlToolTip()
        {

        }
    }
}
